package cn.nubia.admin.sysconfig.controller;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.sysconfig.bean.DictField;
import cn.nubia.admin.sysconfig.query.DictFieldQuery;
import cn.nubia.admin.sysconfig.service.DictFieldService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * Created by Administrator on 2015/7/8.
 */
@Controller
@RequestMapping("/admin/system/dictField")
public class DictFieldController  {
    private final static Logger logger = LoggerFactory.getLogger(DictFieldController.class);
    @Autowired
    private DictFieldService dictFieldService;
    /**
     * 新增枚举接口
     *
     * @param dictField 枚举对象
     * @return 新增的枚举对象.
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Response<DictField>  saveDictField(@Valid DictField dictField,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.getFailedResponse(bindingResult.getFieldError().getDefaultMessage());
        }

        dictFieldService.save(dictField);
        return Response.getSuccessResponse(dictField);
//        return "redirect:/admin/system/dictField/list.html";
    }
    /**
     * 新增枚举接口
     *
     * @return 新增的枚举对象.
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
  //  @RequiresPermissions(value = "dictField:add")
    public ModelAndView addDictField() {
        ModelAndView modelAndView = new ModelAndView("/system/dictFieldForm");
        modelAndView.addObject("dictField",new DictField());
        return modelAndView;
    }
    /**
     * 查看枚举
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
  //  @RequiresPermissions(value = "dictField:edit")
    public ModelAndView view(Integer id) {
        ModelAndView modelAndView = new ModelAndView("/system/dictFieldForm");
        DictField dictField =null;
        try {
            dictField= dictFieldService.view(id);
            modelAndView.addObject("dictField",dictField);
        }  catch (Exception e) {
            logger.error("查看枚举异常:参数"+id, e);
        }
        return modelAndView;
    }

    /**
     * 枚举列表
     * @param dictFieldQuery
     * @return
     */
    @RequiresPermissions("soft:appcfg:dict:dictField:list")
    @RequestMapping(value ="/list", method = RequestMethod.GET)
    public ModelAndView list(DictFieldQuery dictFieldQuery) {
        //参数不正确
        Paged<DictField> dictFieldPaged=null;
        ModelAndView modelAndView = new ModelAndView("/system/dictFieldList");
        try {
            dictFieldPaged=dictFieldService.list(dictFieldQuery);
            modelAndView.addObject("pageList",dictFieldPaged);
            modelAndView.addObject("query",dictFieldQuery);
        }  catch (Exception e) {
            logger.error("枚举查询异常:参数:"+dictFieldQuery.toString(), e);
        }
        return modelAndView;
    }

    /**
     * 删除枚举
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = "dictField:delete")
    public Response<Boolean> delete(Integer id) {
        boolean result=false;
        try {
                dictFieldService.delete(id);
        }  catch (Exception e) {
            logger.error("删除枚举异常:参数"+id, e);
            return Response.getFailedResponse("删除失败",false);
        }
        return Response.getSuccessResponse(true);
    }
    /**
     * 校验枚举
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    @ResponseBody
    public Boolean check(Integer id,String fieldName) {
        boolean result=false;
        try {
            result= dictFieldService.check(id,fieldName);
        }  catch (Exception e) {
            logger.error("校验枚举异常:参数"+id, e);
            return false;
        }

        return result==true?false:true;
    }

}
